// pages/search/search.js
Page({

  data: {
    key_word:"章若楠",
    r_key_word:[
      "温州旅游景点","章芊意","成都五日游","要五星好评文案","杭州出发一日游去哪","爸爸妈妈我们来了","张若瑶","赣南脐橙"
    ],
    flag: 0,
    allNoramalData: [
      {
        id:"101",
        cover: "/images/sucai/_1_王小二cy_来自小红书网页版.jpg",
        title: "#赣州马拉松 #显眼包赣州马拉松的显眼包们 哈哈哈哈哈",
        tx: "/images/sucai/头像.webp",
        name: "王小二cy",
        num: "1000000",
        islike: 1,
        tran_num: "",
        tag: ["穿搭", "推荐"]
      },
      {
        id:"100",
        cover: "/images/sucai/物理老师打篮球一百个只进了三个_1_蛤？_来自小红书网页版.jpg",
        title: "物理老师打篮球一百个只进了三个逊爆了还ikun，但是长的真的很帅#物理老师#坤坤",
        tx: "/images/sucai/1040g2jo30rk4cc0aiq6g5o3altr08utqq3qokq0.webp",
        name: "ha",
        num: "178",
        islike: 1,
        tran_num: "",
        tag: ["穿搭", "职场", "推荐"]
      },
      {
        id:"102",
        cover: "/images/sucai/_1_小红薯673D6FB4_来自小红书网页版 (1).jpg",
        title: "",
        tx: "/images/sucai/645b807d338379fac18930a8.png",
        name: "小红薯673D6FB4",
        num: "888",
        islike: 0,
        tran_num: "",
        tag: ["穿搭", "情感", "推荐"]
      }]
  },
  onLoad(e){
    this.countNum()
  },

  onShow(){
    this.countNum()
    let list = this.data.allNoramalData
    this.setData({
      noramalData:list
    })
  },
  getKey(e){
    console.log(e)
    let key_word = e.currentTarget.dataset.key
    this.setData({
      key_word:key_word
    })
  },
  getSearch(e){
    let key = e.detail.value
    this.setData({
      key_word: key,
      flag:1
    })
  },
  clear(){
    this.setData({
      key_word:"",
      flag: 0
    })
    this.onShow()
  },
  toLike(e){
    let id = e.currentTarget.dataset.id
    let list = this.data.noramalData
    list.forEach(element => {
      if(element.id == id){
        if (element.islike == 1) {
          element.islike = 0
          element.num -= 1
        }else{
          element.islike = 1
          element.num += 1
        }
      }
    this.onShow()
    });
},
countNum(e){
  let list = this.data.allNoramalData
  for(let i = 0;i<list.length;i++) {
    //10000 => 1.0万
    let num = list[i].num
    if (list[i].num>=10000) {
      num = (num / 10000).toFixed(1) + "万"
    }
    list[i].tran_num = num
  }
  this.setData({
    allNoramalData:list
  })
},
toSearch(){
  let user = wx.getStorageSync('userInfo')
  if(user ==  ''){
    wx.navigateTo({
      url: '/pages/login/login',
    })
    }else{
      wx.navigateTo({
        url: '/pages/search/search',
      })
  }
}
})